package com.iraeta.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.Week;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.iraeta.common.Result;
import com.iraeta.entity.WorkLog;
import com.iraeta.service.worklog.WorkLogService;
import com.iraeta.uitl.Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

@RestController(value = "WorkLogController")
@RequestMapping("worklog")
public class WorkLogController extends BaseController{
    private static final Logger logger = LoggerFactory.getLogger(WorkLogController.class);
    @Autowired
    WorkLogService workLogService;

    @RequestMapping("worklogweek")
    public String WorkLogWeek(@RequestBody JSONObject jo){
        String startDateStr = jo.getString("week");
        Date endDate = DateUtil.offsetDay( DateUtil.parseDate(startDateStr), 7);
        List<WorkLog> workLogList = workLogService.getWorkLogByCondition(this.getUserId(), startDateStr, DateUtil.formatDate(endDate));

        List<Map<String, Object>> res = new ArrayList<Map<String, Object>>();
        Date startDate = DateUtil.parseDate(startDateStr);
        int i = 0;
        while (true){
            Date logDate = DateUtil.offsetDay(startDate, i);
            if(logDate.getTime() >= endDate.getTime()){
                break;
            }
            Date logEndDate = DateUtil.offsetDay(logDate, 1);
            List<WorkLog> workLogListForDay = workLogList.stream().filter(x-> x.getLogDate().getTime()>=logDate.getTime()
                    && x.getLogDate().getTime()<logEndDate.getTime()).collect(Collectors.toList());
            if(workLogListForDay.size() > 0){
                HashMap<String,Object> dayMap = new HashMap<String,Object>();
                int weekInt = DateUtil.dayOfWeek(logDate);
                String weekStr = Util.getWeekString(weekInt);
                dayMap.put("logDate", DateUtil.formatDate(logDate) + " " + weekStr);
                dayMap.put("logDateMx", workLogListForDay);
                res.add(dayMap);
            }
            i++;
        }
        return JSON.toJSONString(Result.success(res));
    }

    @RequestMapping("worklogmonth")
    public String workLogMonth(@RequestBody JSONObject jo){
        String month = jo.getString("month");
        String startDateStr = month + "-01";
        Date endDate = DateUtil.offsetMonth( DateUtil.parseDate(startDateStr), 1);
        List<WorkLog> workLogList = workLogService.getWorkLogByCondition(this.getUserId(), startDateStr, DateUtil.formatDate(endDate));

        List<Map<String, Object>> res = new ArrayList<Map<String, Object>>();
        Date startDate = DateUtil.parseDate(startDateStr);
        int i = 0;
        while (true){
            Date logDate = DateUtil.offsetDay(startDate, i);
            if(logDate.getTime() >= endDate.getTime()){
                break;
            }
            Date logEndDate = DateUtil.offsetDay(logDate, 1);
            List<WorkLog> workLogListForDay = workLogList.stream().filter(x-> x.getLogDate().getTime()>=logDate.getTime()
                    && x.getLogDate().getTime()<logEndDate.getTime()).collect(Collectors.toList());
            if(workLogListForDay.size() > 0){
                HashMap<String,Object> dayMap = new HashMap<String,Object>();
                int weekInt = DateUtil.dayOfWeek(logDate);
                String weekStr = Util.getWeekString(weekInt);
                dayMap.put("logDate", DateUtil.formatDate(logDate) + " " + weekStr);
                dayMap.put("logDateMx", workLogListForDay);
                res.add(dayMap);
            }
            i++;
        }
        return JSON.toJSONString(Result.success(res));
    }

    @RequestMapping("getworklog")
    public String getWorkLog(@RequestBody JSONObject paramJb){
        int id = paramJb.getIntValue("id");
        WorkLog workLog = workLogService.getWorkLog(id);
        return JSON.toJSONString(Result.success(workLog));
    }

    @RequestMapping("addeditworklog")
    public String addWorkLog(@RequestBody JSONObject paramJb) throws Exception{
        int id = paramJb.getIntValue("id");
        WorkLog workLog = id == 0 ? new WorkLog() : workLogService.getWorkLog(id);
        this.mapJsonToObj(paramJb, workLog);
        if(id == 0){
            workLog.setUserId(this.getUserId());
            workLogService.addWorkLog(workLog);
        }else {
            workLogService.updateWorkLog(workLog);
        }
        return JSON.toJSONString(Result.success());
    }

    @RequestMapping("deleteworklog")
    public String deleteWorkLog(@RequestBody JSONObject paramJb){
        int id = paramJb.getIntValue("id");
        workLogService.deleteWorkLog(id);
        return JSON.toJSONString(Result.success());
    }
}
